﻿using System;
using System.IO;
using AgFx;
using DepSoft.Mobile.WP7.FollwIt.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{
    /// <summary>
    /// We cache a Movie Summary for 1 month
    /// We do a cache then refresh, because this will give us following principle
    /// 1. App will make request, [A, B, C] will be returned from cache and displayed
    /// 2. AgFx will see that the cache is no longer valid (more than 3600 seconds have elapsed) and will kick off a new request
    /// 3. Server will return, say [C, D, E]
    /// 4. Cache will be replaced and UI will bind to new value
    /// </summary>

    [CachePolicy(CachePolicy.CacheThenRefresh, 3600 * 24 * 31)]
    public class agfxTVShowSummaryViewModel : ModelItemBase<TVShowSummaryLoadContext>
    {
        #region Public bindable properties
        /// <summary>
        /// The <see cref="TVShowDetail" /> property's name.
        /// </summary>
        public const string TVShowDetailPropertyName = "TVShowDetail";

        private TVShowSummary _tvShowDetail = null;

        /// <summary>
        /// Gets the TVShowDetail property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public TVShowSummary TVShowDetail
        {
            get
            {
                return _tvShowDetail;
            }

            set
            {
                if (_tvShowDetail == value)
                {
                    return;
                }

                var oldValue = _tvShowDetail;
                _tvShowDetail = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(TVShowDetailPropertyName);
            }
        }
        #endregion

        #region Constructor
        public agfxTVShowSummaryViewModel()
        {

        }

        public agfxTVShowSummaryViewModel(int tvShowID)
            : base(new TVShowSummaryLoadContext(tvShowID))
        {

        }
        #endregion

        #region AgFx DataLoader
        public class agfxTVShowSummaryViewModelLoader : IDataLoader<TVShowSummaryLoadContext>
        {
            #region IDataLoader implementation
            public object Deserialize(TVShowSummaryLoadContext loadContext, Type objectType, Stream stream)
            {
                var sr = new StreamReader(stream);
                var tvShowSummaryJSON = sr.ReadToEnd();
                JObject tvShowSummaryParsed = JObject.Parse(tvShowSummaryJSON);

                agfxTVShowSummaryViewModel viewModel = new agfxTVShowSummaryViewModel(loadContext.TVShowID);

                JsonSerializer serializer = new JsonSerializer();
                TVShowSummary tvShowSummary = (TVShowSummary)serializer.Deserialize(new JTokenReader(tvShowSummaryParsed), typeof(TVShowSummary));

                viewModel.TVShowDetail = tvShowSummary;

                return viewModel;
            }

            public LoadRequest GetLoadRequest(TVShowSummaryLoadContext loadContext, Type objectType)
            {
                string tvShowSummaryUriBase = FollwItApi.GetInstance().GetActionRestURI(RestAction.GetTVShowSummary).Replace("show_id", string.Format("show_id/{0}", loadContext.TVShowID.ToString()));
                return new WebLoadRequest(loadContext, new Uri(tvShowSummaryUriBase));
            }
            #endregion
        }
        #endregion
    }
}
